package com.lc.cehui.ui.activity.home.search;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.hjq.bar.TitleBar;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.response.StockEntity;
import com.lc.cehui.other.KeyboardWatcher;
import com.lc.cehui.sqlite.RecordsDao;
import com.lc.cehui.widget.ClearEditText;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 企业招聘搜索
 */
public class SearchA1Activity extends AppActivity implements KeyboardWatcher.SoftKeyboardStateListener,
        TextView.OnEditorActionListener{


    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.et_search)
    ClearEditText etSearch;
    @BindView(R.id.ll_seach)
    LinearLayout llSeach;
    @BindView(R.id.rv_popular_searches)
    RecyclerView rvPopularSearches;
    @BindView(R.id.ll_popular_searches)
    LinearLayout llPopularSearches;
    @BindView(R.id.rv_searchRecords)
    RecyclerView rvSearchRecords;
    @BindView(R.id.ll_searchRecords)
    LinearLayout llSearchRecords;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.rv_importantnews)
    RecyclerView rvImportantnews;
    @BindView(R.id.ll_search_content)
    LinearLayout llSearchContent;
    private BaseQuickAdapter<StockEntity, BaseViewHolder> seachHistoryAdapter;
    private BaseQuickAdapter<String, BaseViewHolder> popularadapter;
    private BaseQuickAdapter<String, BaseViewHolder>  searchadapter;
    private List<StockEntity> searchRecordsList;
    private List<StockEntity> tempList;
    private RecordsDao recordsDao;
    List<String> dataPopularBeanList = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_searcha1;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        etSearch.setOnEditorActionListener(this);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.isEmpty(etSearch.getText().toString().trim())){
                    llSearch.setVisibility(View.VISIBLE);
                    llSearchContent.setVisibility(View.GONE);
                }
            }
        });
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext());
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        rvPopularSearches.setLayoutManager(layoutManager);
        popularadapter = new BaseQuickAdapter<String, BaseViewHolder>
                (R.layout.item_search_hot,dataPopularBeanList) {

            @Override
            protected void convert(BaseViewHolder baseViewHolder,String dataBean) {
                baseViewHolder.setText(R.id.tv_search_records,dataBean);
            }


        };
        rvPopularSearches.setAdapter(popularadapter);
//热门
        searchPopular();
        /**
         *   历史记录数据
         */

        recordsDao = new RecordsDao(this);
        searchRecordsList = new ArrayList<>();
        tempList = new ArrayList<>();
        tempList.addAll(recordsDao.findAll());

        reversedList();
        //第一次进入判断数据库中是否有历史记录，没有则不显示
        checkRecordsSize();
//         历史记录
        searchrecords();
        searchadapter=new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_search_hot) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, String jiangliBean) {




            }
        };
        rvImportantnews.setAdapter(searchadapter);
        View emptyview = View.inflate(this, R.layout.view_empty, null);
        searchadapter.setEmptyView(emptyview);
    }
    private void searchPopular() {


//
                dataPopularBeanList.clear();
        dataPopularBeanList.add("高精度测绘仪");
        dataPopularBeanList.add("测绘仪");
        dataPopularBeanList.add("强续航无人机");
                popularadapter.setNewData(dataPopularBeanList);
                popularadapter.notifyDataSetChanged();
                popularadapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

                        etSearch.setText(dataPopularBeanList.get(position));

                        if (!recordsDao.isHasRecord(dataPopularBeanList.get(position))) {
                            //将搜索记录保存至数据库中
                            recordsDao.add(dataPopularBeanList.get(position),"");
                        }
                        tempList.clear();

                        tempList.addAll(recordsDao.findAll());
                        reversedList();
                        checkRecordsSize();
                        seachHistoryAdapter.notifyDataSetChanged();


            }

        });
//
    }
    private void searchrecords() {

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext());
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        rvSearchRecords.setLayoutManager(layoutManager);

        seachHistoryAdapter = new BaseQuickAdapter<StockEntity, BaseViewHolder>
                (R.layout.item_search_hot, searchRecordsList) {

            @Override
            protected void convert(BaseViewHolder baseViewHolder, StockEntity dataBean) {
                baseViewHolder.setText(R.id.tv_search_records, dataBean.getName());
            }


        };
        rvSearchRecords.setAdapter(seachHistoryAdapter);

        seachHistoryAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

                etSearch.setText(searchRecordsList.get(position).getName());


            }
        });

    }
    //当没有匹配的搜索数据的时候不显示历史记录栏
    private void checkRecordsSize() {
        if (searchRecordsList.size() == 0) {
            llSearchRecords.setVisibility(View.GONE);
        } else {
            llSearchRecords.setVisibility(View.VISIBLE);
        }
    }


    //颠倒list顺序，用户输入的信息会从上依次往下显示
    private void reversedList() {
        searchRecordsList.clear();
        for (int i = tempList.size() - 1; i >= 0; i--) {
            searchRecordsList.add(tempList.get(i));
        }
    }
    @OnClick(R.id.tv_searchRecords)
    public void onClick() {
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onSoftKeyboardOpened(int keyboardHeight) {

    }

    @Override
    public void onSoftKeyboardClosed() {

    }
}
